import {Component} from '@angular/core';
import {Storage} from '@ionic/storage';

import {NavController, ToastController} from 'ionic-angular';

import {LoginFormData} from "./login.form.data";
import {UserData} from "../../common/user.data";
import {RegisterPage} from "../register/register";
import {UserService} from "../../service/user/user.service";
import {SmsService} from "../../service/sms/sms.service";


@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
  providers: [LoginFormData, SmsService]
})
export class LoginPage {

  private tab: any = 'PHONE';

  private smsDisable: boolean = false;

  private sendVerificationCodeMessage = "获取验证码";

  private passwordLoginForm: any = {
    cellphone: null,
    password: null
  };
  private fastLoginForm: any = {
    cellphone: null,
    verificationCode: null
  };


  loginFormData: LoginFormData = {};

  submitted = false;

  constructor(private navCtrl: NavController,
              private userService: UserService,
              private user: UserData,
              private smsService: SmsService,
              private toast: ToastController,
              private  storage: Storage) {
  }

  toRegister() {
    this.navCtrl.push(RegisterPage);
  }

  verificationPhone(cellphone: string) {
    return /^1([34578])\d{9}$/.test(cellphone);
    // return this.smsDisable;
  }

  fastLoginBtnEnable(){
    return this.verificationPhone(this.fastLoginForm.cellphone) && this.fastLoginForm.verificationCode;
  }
  passwordLoginBtnEnable(){
    return this.verificationPhone(this.passwordLoginForm.cellphone) && this.passwordLoginForm.password;
  }

  sendVerificationCode() {
    if (!this.verificationPhone(this.fastLoginForm.cellphone)) {
      this.toast.create({
        message: '手机号格式不正确!',
        duration: 2000,
        position: 'top'
      }).present();
      return;
    }
    this.smsDisable = true;
    this.sendVerificationCodeMessage = "正在获取中...";


    this.smsService.sendVerificationCode({cellphone: this.fastLoginForm.cellphone}).subscribe(res => {
      if (res.status === "SUCCESS") {
        this.toast.create({
          message: "验证码发送成功",
          duration: 2000,
          position: 'top'
        }).present();
        let time = 60;
        const timeMessage = setInterval(() => {
          if (time <= 1) {
            clearTimeout(timeMessage);
            this.sendVerificationCodeMessage = "重新获取";
            this.smsDisable = false;
            return;
          }
          this.sendVerificationCodeMessage = "重新获取(" + time + ")";
          time--;
        }, 1000);
      } else {
        this.toast.create({
          message: res.message,
          duration: 2000,
          position: 'top'
        }).present();
        this.sendVerificationCodeMessage = "重新获取";
        this.smsDisable = false;
      }
    });
  }

  private validData() {
    // if (this.tab == 'code'){
    //   this.fastLoginForm.cellphone
    // }
  }

  loginClick() {
    this.validData();
    this.submitted = true;

    if (this.tab == 'PHONE'){
      this.loginFormData.type = 'PHONE';
      this.loginFormData.cellphone = this.fastLoginForm.cellphone;
      this.loginFormData.verificationCode = this.fastLoginForm.verificationCode
    }else {
      this.loginFormData.type="PASSWORD";
      this.loginFormData.cellphone = this.passwordLoginForm.cellphone;
      this.loginFormData.password = this.passwordLoginForm.password
    }

    this.userService.login(this.loginFormData).subscribe(res => {
      if (res.status === "SUCCESS") {
        this.storage.set("user", res.data.userEntity);
        this.storage.set("oauthToken", res.data.oauthToken);
        this.user.token = res.data.oauthToken;
        this.user.user = res.data.userEntity;
        this.user.user.password = this.loginFormData.password;
        // this.navCtrl.push(TabsPage);
        this.navCtrl.pop();
      } else {
        this.toast.create({
          message: res.message,
          duration: 2000,
          position: 'top'
        }).present();
      }
    }, err => {
      this.toast.create({
        message: "登录失败,请重试!",
        duration: 2000,
        position: 'top'
      }).present();
    });
  }
}
